<?php
namespace app\index\model;

use think\Model;

class Brand extends Model{
    protected $pk='brand_id';

//-------------------------------------------------------------------------------    搜索器
    public function searchKeywordsAttr($q,$v,$d){
        $q->where('brand_name','like',"%{$v}%");
    }
//单选推荐搜索器
    public function searchIsHotAttr($q,$v,$d){
        $q->where('recommended','=',$v);
    }
//复选框显示
    public function searchIfShowAttr($q,$v,$d){
        $q->where('if_show','in',$v);
    }
//有图无图搜索器
//    public function searchLogoAttr($q,$v,$d){
//        if($v==1){
//            $q->where('brand_logo','<>','');
//        }else{
//            $q->where(function ($q){
//                $q->whereOr('brand_logo','=','');
//                $q->whereOr('brand_logo','=',null);
//            });
//        }
//    }

//-------------------------------------------------------------------------------------------------------------   品牌展示搜索
    public static function pp($where)
    {
        $list=Brand::withSearch(array_keys($where),$where)
            ->order('sort_order')
            ->paginate([
                'list_rows'=>10,
                'query'=>$where
            ]);

        if($list->count() ==0){
            $list=Brand::withSearch(array_keys($where),$where)
                ->order('sort_order')
                ->paginate([
                    'list_rows'=>10,
                    'query'=>$where,
                    'page'=>$list->lastPage()
                ]);
        }
        return $list;
    }


//-------------------------------------------------------------------------------------------------------------   品牌添加
    public static function add($post)
    {
        try{
            $res=Brand::create($post);   //添加语句
            $msg='添加成功';
        }catch (\Exception $e){
            $res=false;
            $msg=$e->getMessage();
        }
        return [$res,$msg];
    }


//------------------------------------------------------------------------------------------------------------   品牌修改
    public static function edit($post,$id)
    {
        try{
            $res=Brand::update($post,['brand_id'=>$id]);   //添加语句
            $msg='修改成功';
        }catch (\Exception $e){
            $res=false;
            $msg=$e->getMessage();
        }
        return [$res,$msg];
    }


//==================================================品牌修改一条数据
    public static function yitiao($id)
    {
        $list=Brand::where('brand_id','=',$id)->find();
        return $list;
    }
}